#!/usr/bin/env python
#
# convert.py
#   This software is part of a movie DB suite
# Copyright (C) 2010 Kyran Owen-Mankovich

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
                                    
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

#import imdb
#import os

class Movie:
  """Movie

  Sets up structure
  """
  def __init__(self, tt, seen, own, recommendedBy):
    """tt

    Create the mapping for the new DB
    The mapping from the old file format to the new DB format is as follows
       old    = new
       ------------
       imdbid = tt (use text as is)
       status = seen (old format had seen and netflix combined,
                      if "Saw - " then strip and use date as is,
                      if "Netflix" then strip and put Yes/No/Add into netflix)
       own    = own (if yes then change value to DVD,
                     if no then do nothing)
       review = recommendedBy (old format had these fields combined,
                               if "Recommended By" strip that and keep name,
                               if anything else add to review)
    """
    self.tt            = tt
    self.title         = ""
    self.releaseDate   = ""
    self.genre         = ""
    self.actors        = ""
    self.plot          = ""
    self.seen          = seen
    self.own           = own
    self.amazon        = ""
    self.netflix       = ""
    self.recommendedBy = recommendedBy
    self.review        = ""

    ### Old status field was combining seen and netflix
    # Strip "Saw - " if present
    self.seen = seen.replace("Saw - ","")
    # Determine correct netflix values if present
    if self.seen.count("Netflix"):
      if self.seen.count("Add"):
        self.netflix = "Add"
      else:
        self.netflix = "Yes"
      self.seen = ""

    ### Clean-up own
    # For "Yes" or "Own", the value should be DVD,
    # otherwise leave the original text as "No" or "Want"
    self.own = self.own.replace("Yes","DVD").replace("Own", "DVD")

    ### Old review field was combining review and recommended by
    # Strip "Recommended By: " if present
    if self.recommendedBy.count("Recommended By: "):
      self.recommendedBy = self.recommendedBy.replace("Recommended By: ","")
    # Otherwise value should be put in review field
    else:
      self.review        = self.recommendedBy
      self.recommendedBy = ""

  def __str__(self):
    """

    Create an entry formated for the new DB format
    """
    results = [self.tt, self.title, self.releaseDate, self.genre, self.actors,
               self.plot, self.seen, self.own, self.amazon, self.netflix,
               self.recommendedBy, self.review]
    return "::".join(results)

# Open the old formated file for reading
# TODO: make the file name an argument that is passed into convert.py
stream = open("KJs_Movie_DB.data.txt")

# Skip the first four lines in the input file (header stuff)
for n in range(4):
  discard = stream.readline()

# Read the rest of the input file, keeping only the 4 fields of interest
for line in stream.readlines():
  #print "[%s]" % line.strip()
  cols  = line.strip().split("::")

  for n in range(len(cols)):
    if cols[n] == "-":
      cols[n] = ""

  movie = Movie(cols[1], cols[7], cols[8], cols[9])
  print movie

# TODO: make the output goto a file, name supplied by user (for now use redirect)
stream.close()
